/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tardisdatadepositor;

/**
 *
 * @author steve
 */
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
 
public class ConsoleView extends OutputStream
{
        private javax.swing.JTextArea document;
	private ByteArrayOutputStream outputStream = new ByteArrayOutputStream(256);
	private PrintStream ps = null;    
 
	public ConsoleView(javax.swing.JTextArea document, PrintStream ps)
	{
		ConsoleView.this.document = document;
		ConsoleView.this.ps = ps;
	}
 
	public void write(int b)
	{
		outputStream.write (b);
	}
 
	public void flush() throws IOException
	{
		super.flush();
 
		try
		{
			if (document != null)
			{
                            document.setText(document.getText() + new String (outputStream.toByteArray()));
                            document.setCaretPosition( document.getDocument().getLength() );
                        }
			if (ps != null)
			{
				ps.write (outputStream.toByteArray ());
			}
 
			outputStream.reset ();
		}
		catch(Exception e) {}
	}
 
	public static void main(String[] args)
	{
		JTextArea textArea = new JTextArea();
		JScrollPane scrollPane = new JScrollPane( textArea );
 
		JTextField textField = new JTextField();
		textField.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JTextField textField = (JTextField)e.getSource();
				System.out.println( textField.getText() );
				textField.setText("");
			}
		});
 
		JFrame frame = new JFrame("Redirect Output");
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		frame.getContentPane().add( scrollPane );
		frame.getContentPane().add( textField, BorderLayout.SOUTH );
		frame.setSize(300, 600);
		frame.setVisible(true);
 
		/*System.setOut( new PrintStream(
			new ConsoleView (textArea.getDocument (), System.out), true));
		System.setErr( new PrintStream(
			new ConsoleView (textArea.getDocument (), null), true));
                */
            
 
		Timer timer = new Timer(2000, new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.out.println( new java.util.Date().toString() );
				System.err.println( System.currentTimeMillis() );
			}
		});
		timer.start();
	}
}